package com.ruoyi.leave.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.leave.domain.WorkflowWaterPlan;
import com.ruoyi.leave.domain.WorkflowWaterPlanAdjust;
import com.ruoyi.leave.service.IWorkflowWaterPlanAdjustService;
import com.ruoyi.manage.domain.dto.Plan;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.ruoyi.common.constant.CommonResult.DEFAULT_NUM;
import static com.ruoyi.common.constant.CommonResult.DEFAULT_PAGESIZE;

/**
 * 用水计划调整Controller
 *
 * @author ruoyi
 * @date 2024-01-12
 */
@Api(tags = "用水计划调整")

@RestController
@RequestMapping("/workFlow/waterPlanAdjust")
public class WorkflowWaterPlanAdjustController extends BaseController {
    @Autowired
    private IWorkflowWaterPlanAdjustService workflowWaterPlanAdjustService;

    /**
     * 查询用水计划调整列表
     */
    @GetMapping("/adjustReviewList")
    public TableDataInfo list(WorkflowWaterPlanAdjust workflowWaterPlanAdjust) {
        List<WorkflowWaterPlanAdjust> list = workflowWaterPlanAdjustService.selectWorkflowWaterPlanAdjustList(workflowWaterPlanAdjust);
        if (StringUtils.isBlank(workflowWaterPlanAdjust.getPageNum())) {
            workflowWaterPlanAdjust.setPageNum(DEFAULT_NUM + "");
        }
        if (StringUtils.isBlank(workflowWaterPlanAdjust.getPageSize())) {
            workflowWaterPlanAdjust.setPageSize(DEFAULT_PAGESIZE + "");
        }
        list = list.stream().skip((Integer.parseInt(workflowWaterPlanAdjust.getPageNum()) - 1) * Integer.parseInt(workflowWaterPlanAdjust.getPageSize())).limit(Integer.parseInt(workflowWaterPlanAdjust.getPageSize())).collect(Collectors.toList());
        return getDataTable(list);
    }

    @Log(title = "提交申请", businessType = BusinessType.UPDATE)
    @PostMapping("/updatePlanSP")
    public AjaxResult updatePlanSP(@RequestBody WorkflowWaterPlanAdjust workflowWaterPlanAdjust) {

        int i = workflowWaterPlanAdjustService.insertWorkflowWaterPlanAdjust(workflowWaterPlanAdjust);
        if (0 == i) {
            return AjaxResult.error("计划调整记录不存在!!!");
        } else if (2 == i) {
            return AjaxResult.error("请勿重复提交!!!");
        }
        return toAjax(i);
    }

    /**
     * 通过申请
     */
    @Log(title = "通过申请", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/completeAdjustSP/{planId}")
    public AjaxResult completeAdjustSP(@PathVariable("planId") Long planId) {
        int i = workflowWaterPlanAdjustService.completeTaskByPlanID(planId);
        return toAjax(i);
    }


    /**
     * 驳回申请
     */
    @Log(title = "驳回申请", businessType = BusinessType.UPDATE)
    @PostMapping("/rejectPlanSP")
    public AjaxResult rejectPlanSP(@RequestBody Plan plan) {
        int i = workflowWaterPlanAdjustService.rejectTaskByPlanID(plan);
        return toAjax(i);
    }

    /**
     * 审批(工业)
     */
    @Log(title = "审批(工业)", businessType = BusinessType.UPDATE)
    @PostMapping("/examineIndustryAdjust")
    public AjaxResult examineIndustryAdjust(@RequestBody Plan plan) {
        int i = workflowWaterPlanAdjustService.examineIndustryAdjust(plan);
        return toAjax(i);
    }

    /**
     * 审批(农业)
     */
    @Log(title = "审批(农业)", businessType = BusinessType.UPDATE)
    @PostMapping("/examineAgricultureAdjust")
    public AjaxResult examineAgricultureAdjust(@RequestBody Plan plan) {
        int i = workflowWaterPlanAdjustService.examineAgricultureAdjust(plan);
        return toAjax(i);
    }
}
